package com.yuba.front.controller.collect;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.yuba.common.idempotent.annotation.RepeatSubmit;
import com.yuba.common.log.annotation.Log;
import com.yuba.common.web.core.BaseController;
import com.yuba.common.mybatis.core.page.PageQuery;
import com.yuba.common.core.domain.R;
import com.yuba.common.core.validate.AddGroup;
import com.yuba.common.core.validate.EditGroup;
import com.yuba.common.log.enums.BusinessType;
import com.yuba.common.excel.utils.ExcelUtil;
import com.yuba.front.domain.vo.CollectPostsVo;
import com.yuba.front.domain.bo.CollectPostsBo;
import com.yuba.front.service.ICollectPostsService;
import com.yuba.common.mybatis.core.page.TableDataInfo;

/**
 * 帖子收藏
 *
 * @author modakai
 * @date 2024-12-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/collect/collectPosts")
public class CollectPostsController extends BaseController {

    private final ICollectPostsService collectPostsService;

    /**
     * 查询帖子收藏列表
     */
    @SaCheckPermission("collect:collectPosts:list")
    @GetMapping("/list")
    public TableDataInfo<CollectPostsVo> list(CollectPostsBo bo, PageQuery pageQuery) {
        return collectPostsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出帖子收藏列表
     */
    @SaCheckPermission("collect:collectPosts:export")
    @Log(title = "帖子收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CollectPostsBo bo, HttpServletResponse response) {
        List<CollectPostsVo> list = collectPostsService.queryList(bo);
        ExcelUtil.exportExcel(list, "帖子收藏", CollectPostsVo.class, response);
    }

    /**
     * 获取帖子收藏详细信息
     *
     * @param collectId 主键
     */
    @SaCheckPermission("collect:collectPosts:query")
    @GetMapping("/{collectId}")
    public R<CollectPostsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long collectId) {
        return R.ok(collectPostsService.queryById(collectId));
    }

    /**
     * 新增帖子收藏
     */
    @SaCheckPermission("collect:collectPosts:add")
    @Log(title = "帖子收藏", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CollectPostsBo bo) {
        return toAjax(collectPostsService.insertByBo(bo));
    }

    /**
     * 修改帖子收藏
     */
    @SaCheckPermission("collect:collectPosts:edit")
    @Log(title = "帖子收藏", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CollectPostsBo bo) {
        return toAjax(collectPostsService.updateByBo(bo));
    }

    /**
     * 删除帖子收藏
     *
     * @param collectIds 主键串
     */
    @SaCheckPermission("collect:collectPosts:remove")
    @Log(title = "帖子收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{collectIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] collectIds) {
        return toAjax(collectPostsService.deleteWithValidByIds(List.of(collectIds), true));
    }
}
